package trackexplorer.wizard;

import java.io.File;
import java.io.IOException;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class AtlasPage extends WizardPage{
    private Text   text;
    private Button check;

    public AtlasPage(){
        super("Page1");
        setTitle("Atlas name");
        setDescription("Enter atlas name");
    }

    public void createControl(final Composite parent){
        Composite composite = new Composite(parent, SWT.NULL);
        GridLayout gl = new GridLayout();
        gl.numColumns = 2;
        composite.setLayout(gl);
        Label label = new Label(composite, SWT.NONE);
        label.setText("Name:");
        GridData data = new GridData();
        data.widthHint = 100;
        text = new Text(composite, SWT.BORDER);
        text.setLayoutData(data);
        text.addModifyListener(new ModifyListener(){

            public void modifyText(ModifyEvent e){
                getWizard().getContainer().updateButtons();
            }
        });
        new Label(composite, SWT.NONE);
        check = new Button(composite, SWT.CHECK);
        check.setText("Tar atlas");

        Button button = new Button(composite, SWT.NONE);
        button.setText("Load");
        button.addSelectionListener(new SelectionListener(){

            public void widgetDefaultSelected(SelectionEvent e){}

            public void widgetSelected(SelectionEvent event){
                FileDialog dialog = new FileDialog(parent.getShell(), SWT.OPEN);
                dialog.setFilterExtensions(new String[]{"*.xml", "*.*"});
                String open = dialog.open();
                if(open != null){
                    try{
                        AtlasExportWizard wiz = (AtlasExportWizard)getWizard();
                        wiz.load(new File(open));
                        text.setText(wiz.getAtlas());
                        check.setSelection(wiz.getTar());
                        wiz.getContainer().updateButtons();
                    }catch(IOException e){
                        e.printStackTrace();
                    }
                }
            }
        });
        setControl(composite);
        text.setFocus();
    }

    @Override
    public IWizardPage getNextPage(){
        ((AtlasExportWizard)getWizard()).setAtlas(text.getText());
        ((AtlasExportWizard)getWizard()).setTar(check.getSelection());
        LayersPage page = (LayersPage)getWizard().getPage("Page2");
        page.activate();
        return page;
    }

    @Override
    public boolean canFlipToNextPage(){
        return text.getText().length() > 0;
    }
}
